<?php

namespace App\Http\Controllers\Supplier;
use App\Models\ContractRecord;
use App\Models\RechargeSetting;
use App\Models\Resources;
use App\Models\RiskCredit;
use App\Models\ServiceBuyLog;
use App\Models\SetMeal;
use App\Models\ShopRecharge;
use App\Models\ShopRechargeBoolea;
use App\Models\ShopRechargeRadar;
use App\Models\ShopRechargeEdu;
use App\Models\ShopRechargeCre;
use App\Models\ShopSms;
use App\Models\ShopTrans;
use App\Models\ShopUser;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use App\Services\ChargeService;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

//增值服务
class IncrementServiceController extends BaseController
{

    //购买记录(合同/短信)
    public function buyLog(Request $request)
    {
        $input = $request->input();
        if (empty($input['service_type'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '类型为空');
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = ServiceBuyLog::GetServiceBuyLog($pageSize, array('sbl.member_id' => $this->shopUser['member_id'], 'sbl.service_type' => $input['service_type']));
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['buy_type_name'] = ServiceBuyLog::getBuyType($v['buy_type']); //购买类型
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //购买信息(合同/短信)
    public function buyInfo(Request $request)
    {
        $input = $request->input();
        if (empty($input['type'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '类型为空');
        }
        $resources = Resources::where(array('id' => 1))->first();
        $shop_balance = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->value('shop_balance');
        $data['contract'] = $resources['contract'];
        $data['sms_amount'] = $resources['sms_amount'];
        $data['risk_credit_price'] = $resources['risk_credit_price'];
        $data['risk_boolea_credit_price'] = $resources['risk_boolea_credit_price'];
        $data['risk_radar_credit_price'] = $resources['risk_radar_credit_price'];
        $data['risk_cre_credit_price'] = $resources['risk_cre_credit_price'];
        $data['shop_balance'] = $shop_balance;
        $setMeal = SetMeal::GetSetMeal(array('type' => $input['type'], 'status' => SetMeal::SET_MEAL_STATUS_ENABLE));
        $data['setMeal'] = $setMeal;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //去购买(合同/短信)
    public function toBuy(Request $request)
    {
        $input = $request->input();
        if (empty($input['buy_type']) || empty($input['setmeal_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $setMeal = SetMeal::GetOneSetMeal(array('id' => $input['setmeal_id'], 'type' => $input['buy_type'], 'status' => SetMeal::SET_MEAL_STATUS_ENABLE));
        if (empty($setMeal)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '套餐不存在');
        }
        $shop_balance = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->value('shop_balance');
        if ($shop_balance < $setMeal['amount']) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '账户余额不足，请先充值');
        }
        if ($input['buy_type'] == ChargeService::CONTRACT_CHARGE_TYPE) { //1合同 2短信
            $remarks = '合同充值';
            $type_name = 'contract_num';
        } elseif ($input['buy_type'] == ChargeService::CONTRACT_SMS_TYPE) {
            $remarks = '短信充值';
            $type_name = 'sms_num';
        }elseif ($input['buy_type'] == ChargeService::CONTRACT_RISK_CREDIT_TYPE) {
            $remarks = '百融风控充值';
            $type_name = 'risk_credit_amount';
        }elseif ($input['buy_type'] == ChargeService::CONTRACT_RISK_BOOLEA_CREDIT_TYPE) {
            $remarks = '布尔数据充值';
            $type_name = 'risk_credit_amount';
        }elseif ($input['buy_type'] == ChargeService::CONTRACT_RISK_RADAR_CREDIT_TYPE) {
            $remarks = '雷达报告充值';
            $type_name = 'risk_radar_amount';
        }elseif ($input['buy_type'] == ChargeService::CONTRACT_RISK_EDU_CREDIT_TYPE) {
            $remarks = '学历报告充值';
            $type_name = 'risk_edu_amount';
        }elseif ($input['buy_type'] == ChargeService::CONTRACT_RISK_CRE_CREDIT_TYPE) {
            $remarks = '征信报告充值';
            $type_name = 'risk_cre_amount';
        }
        DB::beginTransaction();
        $shop_trans_data = array(
            'member_id' => $this->shopUser['member_id'],
            'amount' => -$setMeal['amount'],
            'before_balance' => $shop_balance,
            'after_balance' => $shop_balance - $setMeal['amount'],
            'trans_type' => ShopTrans::ORDER_TRANS_TYPE_SERVICE_CHARGE,
            'status' => ShopTrans::ORDER_STATUS_FINISH,
            'remarks' => $remarks,
            'create_time' => date('Y-m-d H:i:s'),
        );
        $shop_trans_res = ShopTrans::addData($shop_trans_data);
        $service_buy_log_data = array(
            'member_id' => $this->shopUser['member_id'],
            'service_type' => $input['buy_type'],
            'buy_num' => $setMeal['number'],
            'pay_amount' => $setMeal['amount'],
            'buy_type' => ServiceBuyLog::SERVICE_BUY_LOG_BUY_TYPE_MANUALPAY,
            'user_id' => $this->shopUser['id'],
            'create_time' => date('Y-m-d H:i:s'),
        );
        $service_buy_log_data_res = ServiceBuyLog::addData($service_buy_log_data);
        if ($shop_trans_res && $service_buy_log_data_res) {
            $shop_res = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->decrement('shop_balance', $setMeal['amount']); //减
            if($input['buy_type'] == ChargeService::CONTRACT_RISK_CREDIT_TYPE){
                $recharge_res = ShopRecharge::where(array('member_id' => $this->shopUser['member_id']))->increment($type_name, $setMeal['number']); //加
            }else if($input['buy_type'] == ChargeService::CONTRACT_RISK_BOOLEA_CREDIT_TYPE){
                $recharge_res = ShopRechargeBoolea::where(array('member_id' => $this->shopUser['member_id']))->increment($type_name, $setMeal['number']); //加
            }else if($input['buy_type'] == ChargeService::CONTRACT_RISK_RADAR_CREDIT_TYPE){
                $recharge_res = ShopRechargeRadar::where(array('member_id' => $this->shopUser['member_id']))->increment($type_name, $setMeal['number']); //加
            }else if($input['buy_type'] == ChargeService::CONTRACT_RISK_EDU_CREDIT_TYPE){
                $recharge_res = ShopRechargeEdu::where(array('member_id' => $this->shopUser['member_id']))->increment($type_name, $setMeal['number']); //加
            }else if($input['buy_type'] == ChargeService::CONTRACT_RISK_CRE_CREDIT_TYPE){
                $recharge_res = ShopRechargeCre::where(array('member_id' => $this->shopUser['member_id']))->increment($type_name, $setMeal['number']); //加
            }else{
                $recharge_res = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->increment($type_name, $setMeal['number']); //加
            }
            if ($shop_res && $recharge_res) {
                DB::commit();
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '购买成功');
            } else {
                DB::rollBack();
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '购买失败');
            }
        } else {
            DB::rollBack();
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '购买失败');
        }
    }

    //自动充值信息(合同/短信)
    public function autoRechargeInfo()
    {
        $resources = Resources::where(array('id' => 1))->first();
        $shop_balance = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->value('shop_balance');
        $data['contract'] = $resources['contract'];
        $data['sms_amount'] = $resources['sms_amount'];
        $data['risk_credit_price'] = $resources['risk_credit_price'];
        $data['shop_balance'] = $shop_balance;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //自动充值(合同/短信)
    public function autoRecharge(Request $request)
    {
        $input = $request->input();
        if (empty($input['recharge_type'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '充值类型为空');
        }
        $shopUser = ShopUser::GetShopUser(array('member_id' => $this->shopUser['member_id']));
        if (empty($shopUser)) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '店铺信息为空');
        }
        if ($input['recharge_type'] == 1) { //1合同 2短信
            $type_name = 'contract_automatic';
            $open = ShopUser::SHOP_USER_CONTRACT_AUTOMATIC_OPEN;
            $close = ShopUser::SHOP_USER_CONTRACT_AUTOMATIC_CLOSE;
        } elseif ($input['recharge_type'] == 2) {
            $type_name = 'sms_automatic';
            $open = ShopUser::SHOP_USER_SMS_AUTOMATIC_OPEN;
            $close = ShopUser::SHOP_USER_SMS_AUTOMATIC_CLOSE;
        }
        if ($shopUser[$type_name] == $open) { //合同/短信的自动充值   0关闭  1开启
            $res = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->update(array($type_name => $close));
            if (!$res) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
            }
        } else { //操作开启
            if (empty($input['baseline']) || empty($input['recharge_num']) || empty($input['sms_remind'])) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
            }
            $res = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->update(array($type_name => $open));
            if (!$res) {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
            } else {
                $rechargeSetting = RechargeSetting::GetOneRechargeSetting(array('member_id' => $this->shopUser['member_id'], 'recharge_type' => $input['recharge_type']));
                $recharge_setting_data = array(
                    'member_id' => $this->shopUser['member_id'],
                    'recharge_type' => $input['recharge_type'], //类型   1合同  2短信
                    'baseline' => $input['baseline'], //自动充值触发底线
                    'recharge_num' => $input['recharge_num'], //自动充值数量
                    'sms_remind' => $input['sms_remind'], //是否开启短信提醒  0关闭  1开启
                );
                if (empty($rechargeSetting)) { //添加
                    $recharge_setting_data['create_time'] = date('Y-m-d H:i:s');
                    $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                    $addres = RechargeSetting::addData($recharge_setting_data);
                    if (!$addres) {
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                    } else {
                        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                    }
                } else { //编辑
                    $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                    $editres = RechargeSetting::where(array('member_id' => $this->shopUser['member_id'], 'recharge_type' => $input['recharge_type']))->update($recharge_setting_data);
                    if (!$editres) {
                        return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                    } else {
                        if ($input['recharge_type'] == 1) { //1合同 2短信
                            $sms_template_id = SmsTemplate::SMS_TEMPLATE_GUARANTEE_AUTO_CHARGE_OPEN; //担保合同自动充值开启提醒
                        } elseif ($input['recharge_type'] == 2) {
                            $sms_template_id = SmsTemplate::SMS_TEMPLATE_SMS_AUTO_CHARGE_OPEN; //短信自动充值开启提醒
                        }
                        ShopSms::autoCharge($this->shopUser['member_id'], $sms_template_id);
                        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                    }
                }

            }

        }
    }

    //短信提醒
    public function smsRemind()
    {
        $shopUser = ShopUser::GetShopUser(array('member_id' => $this->shopUser['member_id']));
        $sms_template = SmsTemplate::select('id', 'sms_type', 'sms_title', 'trigger_rule', 'is_charge')->get();
        $sms_template = $sms_template ? $sms_template->toArray() : array();
        $shopSms = ShopSms::select('template_id')->where(array('member_id' => $this->shopUser['member_id'], 'status' => ChargeService::SHOP_SMS_STATUS_OPEN))->get();
        $shopSms = $shopSms ? $shopSms->toArray() : array();
        $template_id_arr = array_column($shopSms, 'template_id');
        $shoper_sms = array();
        $user_sms = array();
        if ($sms_template) {
            foreach ($sms_template as $k => $v) {
                if (in_array($v['id'], $template_id_arr)) {
                    $sms_template[$k]['is_open'] = 1;
                } else {
                    $sms_template[$k]['is_open'] = 0;
                }
                if ($v['sms_type'] == SmsTemplate::SMS_TEMPLATE_SMS_TYPE_SHOPER) { //1供应商提醒  2用户提醒
                    $shoper_sms[] = $sms_template[$k];
                } else {
                    $user_sms[] = $sms_template[$k];
                }
            }
        }
        $data['sms_num'] = $shopUser['sms_num'];
        $data['sms_automatic'] = $shopUser['sms_automatic'];
        $data['shoper_sms'] = $shoper_sms;
        $data['user_sms'] = $user_sms;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //获取单个短信信息
    public function singleSmsInfo(Request $request)
    {
        $input = $request->input();
        if (empty($input['sms_template_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'sms_template_id为空');
        }
        $sms_template = SmsTemplate::GetOneSmsTemplate(array('id' => $input['sms_template_id']));
        $shop_sms = ShopSms::GetOneShopSms(array('member_id' => $this->shopUser['member_id'], 'template_id' => $input['sms_template_id']));
        $contact_tel = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->value('contact_tel');
        $sms_template['is_open'] = isset($shop_sms['status']) && $shop_sms['status'] == ChargeService::SHOP_SMS_STATUS_OPEN ? 1 : 0;
        $sms_template['send_mobile'] = !empty($shop_sms['send_mobile']) ? $shop_sms['send_mobile'] : $contact_tel;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $sms_template);
    }

    //获取批量短信信息
    public function batchSmsInfo(Request $request)
    {
        $input = $request->input();
        if (empty($input['sms_type'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '短信类型为空');
        }
        $sms_template = SmsTemplate::GetSmsTemplate(array('sms_type' => $input['sms_type']));
        $contact_tel = ShopUser::where(array('member_id' => $this->shopUser['member_id']))->value('contact_tel');
        if ($sms_template) {
            foreach ($sms_template as $k => $v) {
                $shop_sms = ShopSms::GetOneShopSms(array('member_id' => $this->shopUser['member_id'], 'template_id' => $v['id']));
                if ($shop_sms) {
                    if ($shop_sms['status'] == ChargeService::SHOP_SMS_STATUS_OPEN) {
                        $sms_template[$k]['is_open'] = 1;
                    } else {
                        $sms_template[$k]['is_open'] = 0;
                    }
                    $sms_template[$k]['send_mobile'] = !empty($shop_sms['send_mobile']) ? $shop_sms['send_mobile'] : $contact_tel;
                } else {
                    $sms_template[$k]['is_open'] = 0;
                    $sms_template[$k]['send_mobile'] = $contact_tel;
                }
            }
        }
        $data = array('sms_type' => $input['sms_type'], 'sms_template' => $sms_template);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //设置单个短信
    public function setSingleSms(Request $request)
    {
        $input = $request->input();
        if (empty($input['sms_template_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'sms_template_id为空');
        }
        if (!empty($input['send_mobile'])) {
            if($input['status'] == 1){
                if (!Verify_Phone($input['send_mobile'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
                }
            }
        }
        $shop_sms = ShopSms::GetOneShopSms(array('member_id' => $this->shopUser['member_id'], 'template_id' => $input['sms_template_id']));
        $shop_sms_data = array(
            'member_id' => $this->shopUser['member_id'],
            'template_id' => $input['sms_template_id'],
            'send_mobile' => $input['send_mobile'],
            'status' => $input['status'],
            'update_time' => date('Y-m-d H:i:s'),
        );
        if (empty($shop_sms)) { //添加
            $res = ShopSms::addData($shop_sms_data);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
            }
        } else { //编辑
            ShopSms::editData(array('member_id' => $this->shopUser['member_id'], 'template_id' => $input['sms_template_id']), $shop_sms_data);
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
        }
    }

    //设置批量短信
    public function setBatchSms(Request $request)
    {
        $input = $request->input();
        if (!empty($input['list']) || is_array(json_decode($input['list'], true))) {
            foreach (json_decode($input['list'], true) as $k => $v) {
                if (empty($v['sms_template_id'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'sms_template_id为空');
                }
                if (!empty($v['send_mobile'])) {
                    if($v['status'] == 1){
                        if (!Verify_Phone($v['send_mobile'])) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请输入有效手机号');
                        }
                    }
                }
                $shop_sms = ShopSms::GetOneShopSms(array('member_id' => $this->shopUser['member_id'], 'template_id' => $v['sms_template_id']));
                $shop_sms_data = array(
                    'member_id' => $this->shopUser['member_id'],
                    'template_id' => $v['sms_template_id'],
                    'send_mobile' => $v['send_mobile'],
                    'status' => $v['status'],
                    'update_time' => date('Y-m-d H:i:s'),
                );
                if (empty($shop_sms)) { //添加
                    ShopSms::addData($shop_sms_data);
                } else { //编辑
                    ShopSms::editData(array('member_id' => $this->shopUser['member_id'], 'template_id' => $v['sms_template_id']), $shop_sms_data);
                }
            }
        }

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
    }

    //获取短信发送场景
    public function getSmsTemplate()
    {
        $sms_template = SmsTemplate::select('id', 'sms_title')->get();
        $sms_template = $sms_template ? $sms_template->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $sms_template);
    }

    //短信发送记录
    public function smsRecord(Request $request)
    {
        $input = $request->input();
        $where = [];
        $where[] = ['sr.member_id', $this->shopUser['member_id']];

        if (!empty($input['mobile'])) {
            $where[] = ['sr.mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }

        if (!empty($input['send_status'])) {
            $where[] = ['sr.send_status', $input['send_status']];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['sr.create_time', '>=', $time['begin_time']];
            $where[] = ['sr.create_time', '<', $time['end_time']];
        }

        if($this->users['role_id']==62){
            $where[] = ['sr.template_id', 23];
        }else if($this->users['role_id']!=62&&!empty($input['template_id'])) {
            $where[] = ['sr.template_id', $input['template_id']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = SmsRecord::GetSmsRecordList($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['is_charge_name'] = SmsRecord::getIsChargee($v['is_charge']); //是否收费
            $info[$k]['send_status_name'] = SmsRecord::getSendStatus($v['send_status']); //发送状态
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 查看店铺百融风控开启状态
     */
    public function getShopRiskCreditStuatus()
    {
        $RiskCredit = ShopRecharge::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_credit_status')->first();
        if ($RiskCredit['risk_credit_status'] == RiskCredit::RISK_CREDIT_OPEN) { //开启
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $RiskCredit);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启担百融风控查询服务,请先开启后再查看该功能');
        }
    }
    /**
     * 查看店铺布尔数据开启状态
     */
    public function getShopRiskBooleaCreditStuatus()
    {
        $RiskCredit = ShopRechargeBoolea::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_credit_status')->first();
        if ($RiskCredit['risk_credit_status'] == RiskCredit::RISK_CREDIT_OPEN) { //开启
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $RiskCredit);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启布尔数据查询服务,请先开启后再查看该功能');
        }
    }
    /**
     * 查看店铺雷达开启状态
     */
    public function getShopRadarCreditCreditStuatus()
    {
        $RiskCredit = ShopRechargeRadar::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_radar_status')->first();
        if ($RiskCredit['risk_radar_status'] == RiskCredit::RISK_CREDIT_OPEN) { //开启
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $RiskCredit);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启雷达报告查询服务,请先开启后再查看该功能');
        }
    }

    /**
     * 查看店铺学历开启状态
     */
    public function getShopEduCreditCreditStuatus()
    {
        $RiskCredit = ShopRechargeEdu::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_edu_status')->first();

        if ($RiskCredit['risk_edu_status'] == RiskCredit::RISK_CREDIT_OPEN) { //开启
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $RiskCredit);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启学历报告查询服务,请先开启后再查看该功能');
        }
    }

    /**
     * 查看店铺征信开启状态
     */
    public function getShopCreCreditCreditStuatus()
    {
        $RiskCredit = ShopRechargeCre::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_cre_status')->first();

        if ($RiskCredit['risk_cre_status'] == RiskCredit::RISK_CREDIT_OPEN) { //开启
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $RiskCredit);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启征信报告查询服务,请先开启后再查看该功能');
        }
    }
    /**
     * 百融风控查询列表
     */
    public function getShopRiskCreditList(Request $request)
    {
        $input = $request->input();
        $RiskCredit = ShopRecharge::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_credit_status','risk_credit_automatic','risk_credit_amount')->first();
        if ($RiskCredit['risk_credit_status'] == RiskCredit::RISK_CREDIT_CLOSE) { //关闭
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启担百融风控查询服务,请先开启后再查看该功能');
        }
        $where = [];
        $where[] = ['rc.member_id', $this->shopUser['member_id']];
        if (!empty($input['order_id'])) {
            $where[] = ['o.trade_no', 'like', '%' . trim($input['order_id']) . '%'];
        }
        if (!empty($input['user_name'])) {
            $where[] = ['rc.user_name', $input['user_name']];
        }
        if (!empty($input['user_mobile'])) {
            $where[] = ['rc.user_mobile', 'like', '%' . trim($input['user_mobile']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['rc.create_time', '>=', $time['begin_time']];
            $where[] = ['rc.create_time', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $RiskCreditList = RiskCredit::getRiskCreditList($page,$pageSize, $where,$this->shopUser['member_id']);
        if(!empty($RiskCreditList)){
            foreach ($RiskCreditList['data'] as $k=>$v){
                $RiskCreditList['data'][$k]['risk_data'] = '验证分'.$v['verification_score'].';欺诈分'.$v['cheat_score'].';'.RiskCredit::YIDUN_RESULT_DATA[$v['final_decision']];
            }
        }
        $data['data'] = $RiskCreditList;
        $data['risk_credit_amount'] = $RiskCredit['risk_credit_amount']; //剩余数量
        $data['risk_credit_automatic'] = $RiskCredit['risk_credit_automatic']; //自动充值
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 布尔数据查询列表
     */
    public function getShopRiskBooleaCreditList(Request $request)
    {
        $input = $request->input();
        $RiskCredit = ShopRechargeBoolea::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_credit_status','risk_credit_automatic','risk_credit_amount')->first();
        if ($RiskCredit['risk_credit_status'] == RiskCredit::RISK_CREDIT_CLOSE) { //关闭
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启布尔数据查询服务,请先开启后再查看该功能');
        }
        $where = [];
        $where[] = ['member_id', $this->shopUser['member_id']];
        if (!empty($input['order_id'])) {
            $where[] = ['order_id', 'like', '%' . trim($input['order_id']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['start_at', '>=', $time['begin_time']];
            $where[] = ['start_at', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $RiskCreditList = RiskCredit::getRiskBooleaCreditList($page,$pageSize, $where);

        if(!empty($RiskCreditList['data'])){
            foreach ($RiskCreditList['data'] as $k=>$v){
                $result = $RiskCreditList['data'][$k]['result'];
                $result = json_decode($result,true);
                if(!empty($result['resp_data']['base_info'])){
                    $RiskCreditList['data'][$k]['name'] = $result['resp_data']['base_info']['name'];
                    $RiskCreditList['data'][$k]['ident_number'] = $result['resp_data']['base_info']['ident_number'];
                    $RiskCreditList['data'][$k]['phone'] = $result['resp_data']['base_info']['phone'];
                }else{
                    $RiskCreditList['data'][$k]['name'] = $RiskCreditList['data'][$k]['realname'];
                    $RiskCreditList['data'][$k]['ident_number'] = '暂无数据';
                    $RiskCreditList['data'][$k]['phone'] = $RiskCreditList['data'][$k]['mobile'];
                }


            }
        }
        // $data['data'] = $RiskCreditList['data'];
        // $data['risk_credit_amount'] = $RiskCredit['risk_credit_amount']; //剩余数量
        // $data['risk_credit_automatic'] = $RiskCredit['risk_credit_automatic']; //自动充值
        $RiskCreditList['risk_credit_amount'] = $RiskCredit['risk_credit_amount']; //剩余数量
        $RiskCreditList['risk_credit_automatic'] = $RiskCredit['risk_credit_automatic']; //自动充值
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $RiskCreditList);
    }
    /**
     * 雷达报告查询列表
     */
    public function getShopRiskRadarCreditList(Request $request)
    {
        $input = $request->input();
        $RiskCredit = ShopRechargeRadar::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_radar_status','risk_radar_automatic','risk_radar_amount')->first();
        if ($RiskCredit['risk_radar_status'] == RiskCredit::RISK_CREDIT_CLOSE) { //关闭
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启雷达报告查询服务,请先开启后再查看该功能');
        }
        $where = [];
        $where[] = ['b.member_id', $this->shopUser['member_id']];
        if (!empty($input['order_id'])) {
            $where[] = ['b.order_id', 'like', '%' . trim($input['order_id']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['b.start_at', '>=', $time['begin_time']];
            $where[] = ['b.start_at', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $RiskCreditList = RiskCredit::getRiskRadarCreditList($page,$pageSize, $where);

        if(!empty($RiskCreditList['data'])){
            foreach ($RiskCreditList['data'] as $k=>$v){
                $RiskCreditList['data'][$k]['cert_name'] = $v['cert_name'];
                $RiskCreditList['data'][$k]['ident_number'] = $v['cert_no'];
                $RiskCreditList['data'][$k]['phone'] = $v['mobile'];

            }
        }
        $data['data'] = $RiskCreditList['data'];
        $data['risk_radar_amount'] = $RiskCredit['risk_radar_amount']; //剩余数量
        $data['risk_radar_automatic'] = $RiskCredit['risk_radar_automatic']; //自动充值
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    /**
     * 学历报告查询列表
     */
    public function getShopRiskEduCreditList(Request $request)
    {
        $input = $request->input();
        $RiskCredit = ShopRechargeEdu::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_edu_status','risk_edu_automatic','risk_edu_amount')->first();
        if ($RiskCredit['risk_edu_status'] == RiskCredit::RISK_CREDIT_CLOSE) { //关闭
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启学历报告查询服务,请先开启后再查看该功能');
        }
        $where = [];
        $where[] = ['b.member_id', $this->shopUser['member_id']];
        if (!empty($input['cert_no'])) {
            $where[] = ['b.cert_no', 'like', '%' . trim($input['cert_no']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['b.start_at', '>=', $time['begin_time']];
            $where[] = ['b.start_at', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $RiskCreditList = RiskCredit::getRiskEduCreditList($page,$pageSize, $where);

        if(!empty($RiskCreditList['data'])){
            foreach ($RiskCreditList['data'] as $k=>$v){
                $RiskCreditList['data'][$k]['cert_name'] = $v['cert_name'];
                $RiskCreditList['data'][$k]['ident_number'] = $v['cert_no'];
                $RiskCreditList['data'][$k]['phone'] = $v['mobile'];

            }
        }
        $data['data'] = $RiskCreditList['data'];
        $data['risk_edu_amount'] = $RiskCredit['risk_edu_amount']; //剩余数量
        $data['risk_edu_automatic'] = $RiskCredit['risk_edu_automatic']; //自动充值
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    /**
     * 征信报告查询列表
     */
    public function getShopRiskCreCreditList(Request $request)
    {
        $input = $request->input();
        $RiskCredit = ShopRechargeCre::where('member_id',$this->shopUser['member_id'])->select('member_id','risk_cre_status','risk_cre_automatic','risk_cre_amount')->first();
        if ($RiskCredit['risk_cre_status'] == RiskCredit::RISK_CREDIT_CLOSE) { //关闭
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该店铺暂未开启征信报告查询服务,请先开启后再查看该功能');
        }
        $where = [];
        $where[] = ['b.member_id', $this->shopUser['member_id']];
        if (!empty($input['cert_no'])) {
            $where[] = ['b.cert_no', 'like', '%' . trim($input['cert_no']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['b.start_at', '>=', $time['begin_time']];
            $where[] = ['b.start_at', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $RiskCreditList = RiskCredit::getRiskCreCreditList($page,$pageSize, $where);

        if(!empty($RiskCreditList['data'])){
            foreach ($RiskCreditList['data'] as $k=>$v){
                $RiskCreditList['data'][$k]['cert_name'] = $v['cert_name'];
                $RiskCreditList['data'][$k]['ident_number'] = substr($v['cert_no'],0,6).'****';
                $RiskCreditList['data'][$k]['cert_no']=$RiskCreditList['data'][$k]['ident_number'];
                $RiskCreditList['data'][$k]['phone'] = substr($v['mobile'],0,3).'****'.substr($v['mobile'],8,4);
                $RiskCreditList['data'][$k]['mobile']=$RiskCreditList['data'][$k]['phone'];


            }
        }
        $data['data'] = $RiskCreditList['data'];
        $data['risk_cre_amount'] = $RiskCredit['risk_cre_amount']; //剩余数量
        $data['risk_cre_automatic'] = $RiskCredit['risk_cre_automatic']; //自动充值
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 获取百融风控购买记录
     */
    public function getRiskCreditBuyLog(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $where[] = ['sbl.service_type',ServiceBuyLog::SERVICE_BUY_LOG_SERVICE_TYPE_RISK];
        $where[] = ['sbl.member_id',$this->shopUser['member_id']];
        $data = ServiceBuyLog::GetServiceBuyLog($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['buy_type_name'] = ServiceBuyLog::getBuyType($v['buy_type']); //购买类型
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 获取布尔数据购买记录
     */
    public function getRiskBooleaCreditBuyLog(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $where[] = ['sbl.service_type',ServiceBuyLog::SERVICE_BUY_LOG_SERVICE_TYPE_RISK_BOOLEA];
        $where[] = ['sbl.member_id',$this->shopUser['member_id']];
        $data = ServiceBuyLog::GetServiceBuyLog($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['buy_type_name'] = ServiceBuyLog::getBuyType($v['buy_type']); //购买类型
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 获取雷达报告购买记录
     */
    public function getRiskRadarCreditBuyLog(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $where[] = ['sbl.service_type',ServiceBuyLog::SERVICE_BUY_LOG_SERVICE_TYPE_RISK_RADAR];
        $where[] = ['sbl.member_id',$this->shopUser['member_id']];
        $data = ServiceBuyLog::GetServiceBuyLog($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['buy_type_name'] = ServiceBuyLog::getBuyType($v['buy_type']); //购买类型
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    /**
     * 获取学历报告购买记录
     */
    public function getRiskEduCreditBuyLog(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $where[] = ['sbl.service_type',ServiceBuyLog::SERVICE_BUY_LOG_SERVICE_TYPE_RISK_EDU];
        $where[] = ['sbl.member_id',$this->shopUser['member_id']];
        $data = ServiceBuyLog::GetServiceBuyLog($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['buy_type_name'] = ServiceBuyLog::getBuyType($v['buy_type']); //购买类型
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    /**
     * 获取征信报告购买记录
     */
    public function getRiskCreCreditBuyLog(Request $request)
    {
        $input = $request->input();
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $where[] = ['sbl.service_type',ServiceBuyLog::SERVICE_BUY_LOG_SERVICE_TYPE_RISK_CRE];
        $where[] = ['sbl.member_id',$this->shopUser['member_id']];
        $data = ServiceBuyLog::GetServiceBuyLog($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['buy_type_name'] = ServiceBuyLog::getBuyType($v['buy_type']); //购买类型
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    /**
     * 百融风控自动充值
     */
    public function riskCreditAutoRecharge(Request $request)
    {
        $input = $request->input();
        $member_id = $this->shopUser['member_id'];
        if(!empty($member_id)){
            $setting = ShopRecharge::where('member_id',$member_id)->select('risk_credit_status','risk_credit_automatic','risk_credit_amount')->first();
            $setting = obToArray($setting);
            if (!empty($setting) && $setting['risk_credit_automatic'] == ShopRecharge::SERVICE_AUTOMATIC_OPEN) { //开启
                $res = ShopRecharge::where(array('member_id' => $member_id))->update(array('risk_credit_automatic' => ShopRecharge::SERVICE_AUTOMATIC_CLOSE));
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                }
            } else { //操作开启
                if (empty($input['baseline']) || empty($input['recharge_num']) || empty($input['sms_remind'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
                }
                if(empty($setting)){
                    $shopRechargeData['member_id'] = $member_id;
                    $shopRechargeData['risk_credit_status'] = RiskCredit::RISK_CREDIT_OPEN;
                    $shopRechargeData['risk_credit_automatic'] = ShopRecharge::SERVICE_AUTOMATIC_OPEN;
                    $shopRechargeData['risk_credit_amount'] = 0;
                    $res = ShopRecharge::insert($shopRechargeData);
                }else{
                    $res = ShopRecharge::where(array('member_id' => $this->shopUser['member_id']))->update(array('risk_credit_automatic' => ShopRecharge::SERVICE_AUTOMATIC_OPEN));
                }
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    $rechargeSetting = RechargeSetting::GetOneRechargeSetting(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT));
                    $recharge_setting_data = array(
                        'member_id' => $this->shopUser['member_id'],
                        'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT,
                        'baseline' => $input['baseline'], //自动充值触发底线
                        'recharge_num' => $input['recharge_num'], //自动充值数量
                        'sms_remind' => $input['sms_remind'], //是否开启短信提醒  0关闭  1开启
                    );
                    if (empty($rechargeSetting)) { //添加
                        $recharge_setting_data['create_time'] = date('Y-m-d H:i:s');
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $addres = RechargeSetting::addData($recharge_setting_data);
                        if (!$addres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    } else { //编辑
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $editres = RechargeSetting::where(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT))->update($recharge_setting_data);
                        if (!$editres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    }
                }
            }
        }else {
            return ReturnMsg(CommonService::HTTP_CODE_NOT_LOGIN, '登陆失效');
        }
    }
    /**
     * 布尔数据自动充值
     */
    public function riskBooleaCreditAutoRecharge(Request $request)
    {
        $input = $request->input();
        $member_id = $this->shopUser['member_id'];
        if(!empty($member_id)){
            $setting = ShopRechargeBoolea::where('member_id',$member_id)->select('risk_credit_status','risk_credit_automatic','risk_credit_amount')->first();
            $setting = obToArray($setting);
            if (!empty($setting) && $setting['risk_credit_automatic'] == ShopRechargeBoolea::SERVICE_AUTOMATIC_OPEN) { //开启
                $res = ShopRechargeBoolea::where(array('member_id' => $member_id))->update(array('risk_credit_automatic' => ShopRechargeBoolea::SERVICE_AUTOMATIC_CLOSE));
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                }
            } else { //操作开启
                if (empty($input['baseline']) || empty($input['recharge_num']) || empty($input['sms_remind'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
                }
                if(empty($setting)){
                    $shopRechargeData['member_id'] = $member_id;
                    $shopRechargeData['risk_credit_status'] = RiskCredit::RISK_CREDIT_OPEN;
                    $shopRechargeData['risk_credit_automatic'] = ShopRechargeBoolea::SERVICE_AUTOMATIC_OPEN;
                    $shopRechargeData['risk_credit_amount'] = 0;
                    $res = ShopRechargeBoolea::insert($shopRechargeData);
                }else{
                    $res = ShopRechargeBoolea::where(array('member_id' => $this->shopUser['member_id']))->update(array('risk_credit_automatic' => ShopRechargeBoolea::SERVICE_AUTOMATIC_OPEN));
                }
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    $rechargeSetting = RechargeSetting::GetOneRechargeSetting(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_BOOLEA));
                    $recharge_setting_data = array(
                        'member_id' => $this->shopUser['member_id'],
                        'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_BOOLEA,
                        'baseline' => $input['baseline'], //自动充值触发底线
                        'recharge_num' => $input['recharge_num'], //自动充值数量
                        'sms_remind' => $input['sms_remind'], //是否开启短信提醒  0关闭  1开启
                    );
                    if (empty($rechargeSetting)) { //添加
                        $recharge_setting_data['create_time'] = date('Y-m-d H:i:s');
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $addres = RechargeSetting::addData($recharge_setting_data);
                        if (!$addres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    } else { //编辑
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $editres = RechargeSetting::where(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_BOOLEA))->update($recharge_setting_data);
                        if (!$editres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    }
                }
            }
        }else {
            return ReturnMsg(CommonService::HTTP_CODE_NOT_LOGIN, '登陆失效');
        }
    }


    /**
     * 雷达报告自动充值
     */
    public function riskRadarCreditAutoRecharge(Request $request)
    {
        $input = $request->input();
        $member_id = $this->shopUser['member_id'];
        if(!empty($member_id)){
            $setting = ShopRechargeRadar::where('member_id',$member_id)->select('risk_radar_status','risk_radar_automatic','risk_radar_amount')->first();
            $setting = obToArray($setting);
            if (!empty($setting) && $setting['risk_radar_automatic'] == ShopRechargeRadar::SERVICE_AUTOMATIC_OPEN) { //开启
                $res = ShopRechargeRadar::where(array('member_id' => $member_id))->update(array('risk_radar_automatic' => ShopRechargeRadar::SERVICE_AUTOMATIC_CLOSE));
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                }
            } else { //操作开启
                if (empty($input['baseline']) || empty($input['recharge_num']) || empty($input['sms_remind'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
                }
                if(empty($setting)){
                    $shopRechargeData['member_id'] = $member_id;
                    $shopRechargeData['risk_radar_status'] = RiskCredit::RISK_CREDIT_OPEN;
                    $shopRechargeData['risk_radar_automatic'] = ShopRechargeRadar::SERVICE_AUTOMATIC_OPEN;
                    $shopRechargeData['risk_radar_amount'] = 0;
                    $res = ShopRechargeRadar::insert($shopRechargeData);
                }else{
                    $res = ShopRechargeRadar::where(array('member_id' => $this->shopUser['member_id']))->update(array('risk_radar_automatic' => ShopRechargeRadar::SERVICE_AUTOMATIC_OPEN));
                }
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    $rechargeSetting = RechargeSetting::GetOneRechargeSetting(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_RADAR));
                    $recharge_setting_data = array(
                        'member_id' => $this->shopUser['member_id'],
                        'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_RADAR,
                        'baseline' => $input['baseline'], //自动充值触发底线
                        'recharge_num' => $input['recharge_num'], //自动充值数量
                        'sms_remind' => $input['sms_remind'], //是否开启短信提醒  0关闭  1开启
                    );
                    if (empty($rechargeSetting)) { //添加
                        $recharge_setting_data['create_time'] = date('Y-m-d H:i:s');
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $addres = RechargeSetting::addData($recharge_setting_data);
                        if (!$addres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    } else { //编辑
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $editres = RechargeSetting::where(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_RADAR))->update($recharge_setting_data);
                        if (!$editres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    }
                }
            }
        }else {
            return ReturnMsg(CommonService::HTTP_CODE_NOT_LOGIN, '登陆失效');
        }
    }

     /**
     * 学历报告自动充值
     */
    public function riskEduCreditAutoRecharge(Request $request)
    {
        $input = $request->input();
        $member_id = $this->shopUser['member_id'];
        if(!empty($member_id)){
            $setting = ShopRechargeEdu::where('member_id',$member_id)->select('risk_edu_status','risk_edu_automatic','risk_edu_amount')->first();
            $setting = obToArray($setting);
            if (!empty($setting) && $setting['risk_edu_automatic'] == ShopRechargeEdu::SERVICE_AUTOMATIC_OPEN) { //开启
                $res = ShopRechargeEdu::where(array('member_id' => $member_id))->update(array('risk_edu_automatic' => ShopRechargeEdu::SERVICE_AUTOMATIC_CLOSE));
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                }
            } else { //操作开启
                if (empty($input['baseline']) || empty($input['recharge_num']) || empty($input['sms_remind'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
                }
                if(empty($setting)){
                    $shopRechargeData['member_id'] = $member_id;
                    $shopRechargeData['risk_edu_status'] = RiskCredit::RISK_CREDIT_OPEN;
                    $shopRechargeData['risk_edu_automatic'] = ShopRechargeEdu::SERVICE_AUTOMATIC_OPEN;
                    $shopRechargeData['risk_edu_amount'] = 0;
                    $res = ShopRechargeEdu::insert($shopRechargeData);
                }else{
                    $res = ShopRechargeEdu::where(array('member_id' => $this->shopUser['member_id']))->update(array('risk_edu_automatic' => ShopRechargeEdu::SERVICE_AUTOMATIC_OPEN));
                }
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    $rechargeSetting = RechargeSetting::GetOneRechargeSetting(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_EDU));
                    $recharge_setting_data = array(
                        'member_id' => $this->shopUser['member_id'],
                        'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_EDU,
                        'baseline' => $input['baseline'], //自动充值触发底线
                        'recharge_num' => $input['recharge_num'], //自动充值数量
                        'sms_remind' => $input['sms_remind'], //是否开启短信提醒  0关闭  1开启
                    );
                    if (empty($rechargeSetting)) { //添加
                        $recharge_setting_data['create_time'] = date('Y-m-d H:i:s');
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $addres = RechargeSetting::addData($recharge_setting_data);
                        if (!$addres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    } else { //编辑
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $editres = RechargeSetting::where(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_EDU))->update($recharge_setting_data);
                        if (!$editres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    }
                }
            }
        }else {
            return ReturnMsg(CommonService::HTTP_CODE_NOT_LOGIN, '登陆失效');
        }
    }

     /**
     * 征信报告自动充值
     */
    public function riskCreCreditAutoRecharge(Request $request)
    {
        $input = $request->input();
        $member_id = $this->shopUser['member_id'];
        if(!empty($member_id)){
            $setting = ShopRechargeCre::where('member_id',$member_id)->select('risk_cre_status','risk_cre_automatic','risk_cre_amount')->first();
            $setting = obToArray($setting);
            if (!empty($setting) && $setting['risk_cre_automatic'] == ShopRechargeCre::SERVICE_AUTOMATIC_OPEN) { //开启
                $res = ShopRechargeCre::where(array('member_id' => $member_id))->update(array('risk_cre_automatic' => ShopRechargeCre::SERVICE_AUTOMATIC_CLOSE));
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                }
            } else { //操作开启
                if (empty($input['baseline']) || empty($input['recharge_num']) || empty($input['sms_remind'])) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
                }
                if(empty($setting)){
                    $shopRechargeData['member_id'] = $member_id;
                    $shopRechargeData['risk_cre_status'] = RiskCredit::RISK_CREDIT_OPEN;
                    $shopRechargeData['risk_cre_automatic'] = ShopRechargeCre::SERVICE_AUTOMATIC_OPEN;
                    $shopRechargeData['risk_cre_amount'] = 0;
                    $res = ShopRechargeCre::insert($shopRechargeData);
                }else{
                    $res = ShopRechargeCre::where(array('member_id' => $this->shopUser['member_id']))->update(array('risk_cre_automatic' => ShopRechargeCre::SERVICE_AUTOMATIC_OPEN));
                }
                if (!$res) {
                    return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                } else {
                    $rechargeSetting = RechargeSetting::GetOneRechargeSetting(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_CRE));
                    $recharge_setting_data = array(
                        'member_id' => $this->shopUser['member_id'],
                        'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_CRE,
                        'baseline' => $input['baseline'], //自动充值触发底线
                        'recharge_num' => $input['recharge_num'], //自动充值数量
                        'sms_remind' => $input['sms_remind'], //是否开启短信提醒  0关闭  1开启
                    );
                    if (empty($rechargeSetting)) { //添加
                        $recharge_setting_data['create_time'] = date('Y-m-d H:i:s');
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $addres = RechargeSetting::addData($recharge_setting_data);
                        if (!$addres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    } else { //编辑
                        $recharge_setting_data['update_time'] = date('Y-m-d H:i:s');
                        $editres = RechargeSetting::where(array('member_id' => $member_id, 'recharge_type' => RechargeSetting::RECHARGE_TYPE_RISKCREDIT_CRE))->update($recharge_setting_data);
                        if (!$editres) {
                            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '设置失败');
                        } else {
                            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '设置成功');
                        }
                    }
                }
            }
        }else {
            return ReturnMsg(CommonService::HTTP_CODE_NOT_LOGIN, '登陆失效');
        }
    }

}
